package cn.lili.controller.user;

import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.member.entity.dto.UserEditBaseDTO;
import cn.lili.modules.member.entity.vo.UserVO;
import cn.lili.modules.member.service.UserSecurityService;
import cn.lili.modules.member.service.UserService;
import cn.lili.modules.order.order.entity.dto.UserFlowDTO;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 用户信息接口
 *
 * @author liushuai(liushuai711 @ gmail.com)
 * @version v4.0
 * @Description:
 * @since 2022/8/23 16:33
 */
@RestController
@Api(tags = "用户信息接口")
@RequestMapping("/user/info")
@RequiredArgsConstructor
public class UserinfoController {


    private final UserService userService;
    private final UserSecurityService userSecurityService;

    @ApiOperation(value = "获取当前登录用户接口")
    @GetMapping
    public UserVO getUserInfo() {
        return userService.userInfo();
    }


    @ApiOperation(value = "修改用户自己资料")
    @PutMapping("/edit")
    public ResultMessage<User> editOwn(UserEditBaseDTO userEditBaseDTO) {
        return ResultUtil.data(userService.editOwn(userEditBaseDTO));
    }

    @ApiOperation(value = "修改密码")
    @ApiImplicitParams({@ApiImplicitParam(name = "password", value = "旧密码", required = true, paramType = "query"), @ApiImplicitParam(name = "newPassword", value = "新密码", required = true, paramType = "query")})
    @PutMapping("/modify/pass")
    public ResultMessage<User> modifyPass(@NotNull(message = "旧密码不能为空") @RequestParam String password, @NotNull(message = "新密码不能为空") @RequestParam String newPassword) {
        return ResultUtil.data(userSecurityService.modifyPass(password, newPassword));
    }

    /**
     * 分销商入驻
     *
     * @param user 用户信息
     * @return 是否成功
     */
    @PostMapping("/supplier/save")
    public ResultMessage<Object> supplierSave(@RequestBody User user) {
        return ResultUtil.data(userService.supplierSave(user));
    }

    /**
     * 根据邀请人ID查询下级用户(直推用户)
     * @param parentId 邀请人ID
     * @return 直推用户集合
     * */
    @GetMapping("/demo/getLowerByParentId")
    public ResultMessage<List<User>> getLowerByParentId(@RequestParam String parentId) {
        return ResultUtil.data(userService.getLowerByParentId(parentId));
    }

    /**
     * 根据邀请人ID查询下级用户(直推用户)
     * @param parentId 邀请人ID
     * @return 直推用户集合
     * */
    @GetMapping("/demo/getCountLowerByParentId")
    public ResultMessage<Integer> getCountLowerByParentId(@RequestParam String parentId) {
        return ResultUtil.data(userService.getCountLowerByParentId(parentId));
    }

    /**
     * 根据邀请人ID查询下级用户（间推用户）
     * @param parentId 邀请人ID
     * @return 间推用户集合
     * */
    @GetMapping("/demo/getLowerSubByParentId")
    public ResultMessage<List<User>> getLowerSubByParentId(@RequestParam String parentId) {
        return ResultUtil.data(userService.getLowerSubByParentId(parentId));
    }

    /**
     * 根据邀请人ID查询下级用户(直推用户)
     * @param parentId 邀请人ID
     * @return 直推用户集合
     * */
    @GetMapping("/demo/getCountLowerSubByParentId")
    public ResultMessage<Integer> getCountLowerSubByParentId(@RequestParam String parentId) {
        return ResultUtil.data(userService.getCountLowerSubByParentId(parentId));
    }

    /**
     * 根据邀请人ID和时间查询直推用户及间推用户+用户类型+分销类型+业绩+收益
     * @param parentId 邀请人ID
     * @param flowTime 计算流水时间-年月 如：2024-01
     * @param profitSharing 结算状态
     * @return 直推用户及间推用户
     * */
    @GetMapping("/demo/getLowerAndLowerSubByParentId")
    public ResultMessage<List<UserFlowDTO>> getLowerAndLowerSubByParentId(@RequestParam String parentId, @RequestParam String flowTime,@RequestParam String profitSharing) {
        return ResultUtil.data(userService.getLowerAndLowerSubByParentId(parentId,flowTime,profitSharing));
    }

    /**
     * 查询团队树
     * @param parentId 邀请人ID
     * @param flowTime 计算流水时间-年月 如：2024-01
     * @param profitSharing 结算状态
     * @return 直推用户及间推用户
     * */
    @GetMapping("/demo/getLowerAndLowerSubByParentIdTree")
    public ResultMessage<List<UserFlowDTO>> getLowerAndLowerSubByParentIdTree(@RequestParam String parentId, @RequestParam String flowTime,@RequestParam String profitSharing) {
        return ResultUtil.data(userService.getLowerAndLowerSubByParentIdTree(parentId,flowTime,profitSharing));
    }

    @GetMapping("/demo/queryShareGoodsBindParent")
    @ApiOperation("查询redis里分享链接关系")
    public ResultMessage<String> queryShareGoodsBindParent(@RequestParam("redisKey") String redisKey) {
        return ResultUtil.data(userService.queryShareGoodsBindParent(redisKey));
    }

}
